package com.cyl.springboot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyl.springboot.entity.PowerBank;
import com.cyl.springboot.entity.Site;
import com.cyl.springboot.mapper.PowerBankMapper;
import com.cyl.springboot.service.IPowerBankService;
import com.cyl.springboot.service.ISiteService;
import com.cyl.springboot.vo.PowerBankVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PowerBankServiceImpl extends ServiceImpl<PowerBankMapper, PowerBank> implements IPowerBankService {

    @Autowired
    private ISiteService iSiteService;

    @Resource
    private PowerBankMapper powerBankMapper;

    /**
     * 获取充电宝列表
     * @param pageNo 页码
     * @param pageSize 一页多少条
     * @param siteId 站点ID
     * @param role 角色
     * @return
     */
    @Override
    public Page<PowerBankVo> getPowerBankList(Integer pageNo, Integer pageSize, Integer siteId, String role) {
        Page page = new Page(pageNo, pageSize);
        LambdaQueryWrapper<PowerBank> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Site> siteLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 不是管理员只能看自己下面的信息
        if (!role.equals("ROLE_ADMIN")) {
            lambdaQueryWrapper.eq(PowerBank::getSiteId, siteId);

        }
        // 如果不传角色，那么就认为是在查询充电宝数量
        if (StrUtil.isEmpty(role)) {
            return page.setRecords(powerBankMapper.findNums(siteId));
        }
        siteLambdaQueryWrapper.eq(Site::getId, siteId);
        Site site = iSiteService.getOne(siteLambdaQueryWrapper);
        lambdaQueryWrapper.orderByDesc(PowerBank::getCreateTime);
        Page pages = this.page(page, lambdaQueryWrapper);
        List<PowerBank> records = pages.getRecords();
        List<PowerBankVo> result = records.stream().map(t -> {
            PowerBankVo powerBankVo = new PowerBankVo();
            BeanUtil.copyProperties(t, powerBankVo, true);
            // 计算时间
            Long day = LocalDate.now().toEpochDay() - t.getCreateTime().toEpochDay();
            powerBankVo.setUseTime(day);
            powerBankVo.setSiteName(site.getSiteName());
            return powerBankVo;
        }).collect(Collectors.toList());
        pages.setRecords(result);
        return pages;
    }

    /**
     *
     * 获取充电宝列表
     * @param pageNo 页码
     * @param pageSize 一页多少条
     * @param userId 站点ID
     * @param role 角色
     * @param name 充电宝名称
     * @return
     */
    @Override
    public Page<PowerBankVo> getPowerBankAll(Integer pageNo, Integer pageSize, Integer userId, String role,String name) {
        Page page = new Page(pageNo, pageSize);
        LambdaQueryWrapper<PowerBank> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Site> siteLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 不是管理员只能看自己下面的信息
        if (!role.equals("ROLE_ADMIN")) {
            lambdaQueryWrapper.eq(PowerBank::getUserId, userId);
        }
        // 充电宝名称模糊查询
        if(StrUtil.isNotBlank(name)){
            lambdaQueryWrapper.eq(PowerBank::getName,name);
        }
        siteLambdaQueryWrapper.eq(Site::getUserId, userId);
        List<Site> siteList = iSiteService.list(siteLambdaQueryWrapper);
        lambdaQueryWrapper.orderByDesc(PowerBank::getCreateTime);
        Page pages = this.page(page, lambdaQueryWrapper);
        List<PowerBank> records = pages.getRecords();
        List<PowerBankVo> result = records.stream().map(t -> {
            PowerBankVo powerBankVo = new PowerBankVo();
            BeanUtil.copyProperties(t, powerBankVo, true);
            // 计算时间
            Long day = LocalDate.now().toEpochDay() - t.getCreateTime().toEpochDay();
            powerBankVo.setUseTime(day);
            return powerBankVo;
        }).collect(Collectors.toList());
        // 找到对应站点的名字
        siteList.forEach(s -> {
            result.forEach(res -> {
                if (s.getId().equals(Integer.parseInt(res.getSiteId()))) {
                    res.setSiteName(s.getSiteName());
                }
            });
        });
        pages.setRecords(result);
        return pages;
    }


}
